//
//  HTPremiumPaySection.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/8.
//

import SwiftUI

struct HTPremiumPaySection: View {

  @EnvironmentObject private var var_store: HTAppStore

  var body: some View {
    VStack(spacing: 0){
      ZStack{
        Button {
          var_store.dispatch(HTEnumSubscribeAction.ht_purchaseProduct(productId: var_store.state.var_subscribeState.var_selSubscribe!.var_productIdentifier ?? "", andSuccess: nil, andFail: nil))
          if (var_store.state.var_subscribeState.var_selSubscribe?.var_introductoryPrice != nil) {
            HTNetworkManager().ht_requestForBuried(.htEnumVipCl(var_kid: "9", var_source: "7"))
          } else {
            if (var_store.state.var_subscribeState.var_selSubscribe!.var_productIdentifier == "week") {
              HTNetworkManager().ht_requestForBuried(.htEnumVipCl(var_kid: "7", var_source: "7"))
            } else if (var_store.state.var_subscribeState.var_selSubscribe!.var_productIdentifier == "month") {
              HTNetworkManager().ht_requestForBuried(.htEnumVipCl(var_kid: "2", var_source: "7"))
            } else if (var_store.state.var_subscribeState.var_selSubscribe!.var_productIdentifier == "year") {
              HTNetworkManager().ht_requestForBuried(.htEnumVipCl(var_kid: "3", var_source: "7"))
            }
          }
        } label: {
          HStack{
            Text("Pay $\(var_store.state.var_subscribeState.var_selSubscribe!.ht_getPayPrice()!)")
              .font(.system(size: 20))
          }
          .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        .frame(maxWidth: .infinity, minHeight: 44, maxHeight: 44)
        .foregroundColor(Color(hex: "#685034"))
        .background((LinearGradient(colors: [Color(hex: "#EDC391"), Color(hex: "#FDDDB7")], startPoint: .leading, endPoint: .trailing)))
      }
      .clipShape(Capsule())


      Spacer().frame(height: 10)
      HStack(spacing: 0){
        Group{
          Text("If the ad still appears after purchase, tap ")
            .font(.system(size: 14))
            .foregroundColor(Color(hex: "#999999"))
          Text("Restore.")
            .underline()
            .font(.system(size: 14))
            .foregroundColor(Color(hex: "#999999"))
            .onTapGesture {
              var_store.dispatch(HTEnumSubscribeAction.ht_retrieveProductsInfo())
              var_store.dispatch(HTEnumSubscribeAction.ht_restorePurchases())
              HTNetworkManager().ht_requestForBuried(.htEnumVipCl(var_kid: "5", var_source: "7"))
            }
        }
      }
    }
    .padding(.horizontal, 15)
    .padding(.vertical, 10)
    .background(Color.white)
    .frame(maxWidth: .infinity)
  }
}
